JavaScript Arrays


What is an Array?

An array in JavaScript is a data structure that allows you to store multiple values in a single variable. These values can be of any data type, including numbers, strings, booleans, objects, or even other arrays.

Arrays are ordered and indexed starting from zero.


Creating an Array


1. Using Array Literal

let fruits = ["Apple", "Banana", "Cherry"];

2. Using the new Array() Constructor

let numbers = new Array(1, 2, 3, 4);

It is recommended to use array literals for simplicity and clarity.


Accessing Elements

Use bracket notation with an index to access array elements.

let colors = ["Red", "Green", "Blue"];
console.log(colors[0]); // Output: Red
console.log(colors[1]); // Output: Green

Modifying Elements

colors[1] = "Yellow";
console.log(colors); // Output: ["Red", "Yellow", "Blue"]

Array Length

The length property returns the number of elements in the array.

console.log(colors.length); // Output: 3

Adding and Removing Elements


push() – Add to end

colors.push("Purple");
console.log(colors); // Output: ["Red", "Yellow", "Blue", "Purple"]

pop() – Remove from end

colors.pop();
console.log(colors); // Output: ["Red", "Yelow", "Blue"]

unshift() – Add to beginning

colors.unshift("Black");
console.log(colors); // Output: ["Black", "Red", "Yellow", "Blue"]

shift() – Remove from beginning

colors.shift();
console.log(colors); // Output: ["Red", "Yellow", "Blue"]

Looping Through an Array


Using for loop

for (let i = 0; i < colors.length; i++) {
console.log(colors[i]);
}

Using for...of loop

for (let color of colors) {
console.log(color);
}

Array Methods

Method Description Example Output
push() Add item to end arr.push(5) [... , 5]
pop() Remove item from end arr.pop() Removes last element
shift() Remove item from start arr.shift() Removes first element
unshift() Add item to start arr.unshift(0) [0, ...]
concat() Merge arrays arr1.concat(arr2) New merged array
join() Join elements into string arr.join(", ") String with comma separator
slice() Extract portion of array arr.slice(1, 3) Elements from index 1 to 2
splice() Add/remove items at specific index arr.splice(1, 2, "new") Modify original array
indexOf() Find index of item arr.indexOf("item" ) Returns index or -1
includes() Check if item exists arr.includes("item ") true or false
reverse() Reverse the array arr.reverse() Original array reversed
sort() Sort array arr.sort() Sorted array

Example: Using splice()

let numbers = [1, 2, 3, 4, 5];
numbers.splice(2, 1, 99); // Remove 1 item at index 2, add 99
console.log(numbers); // Output: [1, 2, 99, 4, 5]

Nested Arrays (Multi-dimensional)

let matrix = [
[1, 2],
[3, 4],
[5, 6]
];
console.log(matrix[1][0]); // Output: 3

Arrays are Objects

Arrays are technically special types of objects, where:

  • Indexes are keys (0, 1, 2...)
  • The typeof operator returns "object"
let arr = [10, 20, 30];
console.log(typeof arr); // Output: object

Array vs Object

Feature Array Object
Indexing Numbered (0, 1, 2...) Named keys ("name" , "age")
Order Ordered Not guaranteed
Best Use List of items